package com.neiquan.backstage.scene_visit_reservation.web.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.BackendErrorCode;
import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Paging;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.common.util.Strings;
import com.neiquan.backstage.pub.bsc.dao.po.AdminUser;
import com.neiquan.backstage.pub.bsc.dao.po.SceneVisitReservation;
import com.neiquan.backstage.pub.bsc.dao.po.User;
import com.neiquan.backstage.resourec_manage.EntrepreneurService;
import com.neiquan.backstage.scene_visit_reservation.SceneVisitReservationService;

@Controller
@RequestMapping(value = "sceneVisitReservation", produces = { "text/json;charset=UTF-8" })
public class SceneVisitReservationAction {
	private static Class<?> poc = SceneVisitReservation.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private SceneVisitReservationService sceneVisitReservationService;
	@Autowired
	private EntrepreneurService entrepreneurService;

	@ResponseBody
	@RequestMapping("list")
	public String list(int pageIndex, int pageSize, HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Criterion criterion = Restrictions.eq("memberId", loginedUser.getMemberId());
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("id"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "queryDetail")
	public String queryDetail(Integer id) {
		return CommonResp.buildSuccessResp(sceneVisitReservationService.queryById(id)).toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "remove")
	public String remove(Integer id) {
		SceneVisitReservation toDelete = sceneVisitReservationService.queryById(id);
		if (toDelete == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		extraSpringHibernateTemplate.getHibernateTemplate().delete(toDelete);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String name, String entrepreneurName, HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		List<Criterion> criterions = new ArrayList<>();
		criterions.add(Restrictions.eq("memberId", loginedUser.getMemberId()));
		if (StringUtils.isNoneBlank(name)) {
			criterions.add(Restrictions.like("name", "%" + name + "%"));
		}
		if (StringUtils.isNoneBlank(entrepreneurName)) {
			criterions.add(Restrictions.like("entrepreneurName", "%" + entrepreneurName + "%"));
		}
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("sceneVisitReservationId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
	
	@ResponseBody
	@RequestMapping(value = "add")
	public String add(String entrepreneurName,Integer entrepreneurId,String name, String phone, Integer peopleCount, String remark, HttpSession session) {
		User user = (User) Sessions.getLoginedUser(session);
		SceneVisitReservation toSave = new SceneVisitReservation();
		toSave.setStatus(SceneVisitReservation.status_wait);
		toSave.setMemberId(user.getMemberId());
		toSave.setDtCreat(System.currentTimeMillis());
		toSave.setEntrepreneurId(entrepreneurId);
		toSave.setEntrepreneurName(entrepreneurService.queryById(entrepreneurId).getName());
		toSave.setName(name);
		toSave.setUserId(user.getUserId());
		toSave.setPeopleCount(peopleCount);
		toSave.setPhone(phone);
		toSave.setRemark(remark);
		toSave.setApplyType(SceneVisitReservation.apply_scene);
		extraSpringHibernateTemplate.getHibernateTemplate().save(toSave);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "edit")
	public String edit(Integer id, Integer entrepreneurId, String name, String phone, Integer peopleCount, String remark) {
		SceneVisitReservation toUpdate = sceneVisitReservationService.queryById(id);
		if (null == toUpdate) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		if (entrepreneurId != null) {
			toUpdate.setEntrepreneurId(entrepreneurId);
			toUpdate.setEntrepreneurName(entrepreneurService.queryById(entrepreneurId).getName());
		}
		if (StringUtils.isNoneBlank(name)) {
			toUpdate.setName(Strings.trimAndNotNull(name));
		}
		if (StringUtils.isNoneBlank(phone)) {
			toUpdate.setPhone(Strings.trimAndNotNull(phone));
		}
		if (peopleCount != null) {
			toUpdate.setPeopleCount(peopleCount);
		}
		if (StringUtils.isNoneBlank(remark)) {
			toUpdate.setRemark(Strings.trimAndNotNull(remark));
		}
		extraSpringHibernateTemplate.getHibernateTemplate().update(toUpdate);
		return CommonResp.buildSuccessResp().toJsonString();
	}
}
